package cn.jilian.meta.handler.generators;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;

import cn.jilian.entity.MField;
import cn.jilian.entity.Meta;
import cn.jilian.meta.handler.CompGenerator;
import cn.jilian.meta.handler.GeneratorManager;
import cn.jilian.meta.handler.constants.HandlerConstants;
import cn.jilian.meta.handler.domain.TreeModel;
import cn.jilian.meta.handler.domain.XField;

public class TreeModelGenerator implements CompGenerator<TreeModel> {

	private GeneratorManager manager;
	public TreeModelGenerator(GeneratorManager manager) {
		this.manager=manager;
	}
	@Override
	public TreeModel convert(Meta meta, Map<String, Object> extConfig) {
		TreeModel treeModel=new TreeModel();
		treeModel.setExtend("Ext.data.TreeModel");
		List<MField> fs=meta.getFields();
		
		if(CollectionUtils.isNotEmpty(fs)) {
			Collection<Object> fields=new LinkedHashSet<>();
			for(MField f:fs) {
				XField field=new XField();
				field.setName(f.getName());
				field.setType(HandlerConstants.typeMap.get(f.getType()));
				fields.add(field);
			}
			treeModel.setFields(fields);
		}
		return treeModel;
	}

}
